//
//  CourseViewController.m
//  Civacrm
//
//  Created by 王桂敏 on 2017/2/4.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "CourseViewController.h"
#import "TaskTableViewCell.h"
#import "CourseModel.h"
#import "CourseDetailViewController.h"



static NSString *TaskIdentifier = @"TaskCell";
@interface CourseViewController ()

@property (nonatomic,strong) NSMutableArray *dataSource;

@end

@implementation CourseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initData];
    [self initNavigation];
    [self initView];
    [self initNetWork];
    
}
- (void)initNavigation{
    self.title = @"课程";
}
- (void)initData{
    self.dataSource = [NSMutableArray array];
}
- (void)initView{
    
    self.tableView.frame = CGRectMake(0, 0, self.view.width, self.view.height - 64);
    self.tableView.backgroundColor = ClearColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[TaskTableViewCell  class] forCellReuseIdentifier:TaskIdentifier];
    self.nillView = [[NillView alloc] initViewWithType:NillViewType0 frame:self.view.bounds];
    @weakify(self);
    self.nillView.blockOfbackground = ^(){
        @strongify(self);
        [self initNetWork];
    };
    [self.view addSubview:self.nillView];
}

#pragma mark - UITableViewDataSource&UITableViewDelegate -
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    CourseDetailViewController *VC = [[CourseDetailViewController alloc] init];
    VC.webURL = [NSString stringWithFormat:@"%@&uId=%@&taskId=%@",H5url,self.userID,[[self.dataSource[indexPath.row] taskModel] taskId]];
    VC.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:VC animated:YES];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataSource.count) {
        return self.dataSource.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [(NSArray *)[self.dataSource[indexPath.row] userInfoArray] count] ? h(252) : h(140);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TaskIdentifier];
    [cell configCellWithMode:self.dataSource[indexPath.row]];
    
    return cell;
}

#pragma mark - networkAndNetworkDelegate -
- (void)initNetWork{
    
    self.params[@"uId"] = self.userID;
    [self showIndeterminate];
    [self.manager postWithURL:NET_SHOWTASKLIST param:self.params delegate:self isEncrypt:NO type:NetWorkingType0];
    
}

- (void)networkingHandleSuccess:(NetworkingHandle *)handle parameters:(NSDictionary *)parameters responseObject:(id)responseObject type:(NetWorkingType)type{
    [self hideHud];
    switch (type) {
        case NetWorkingType0:
        {
            NSArray *dataArray = responseObject;
            [self.nillView removeFromSuperview];
            [self.dataSource removeAllObjects];
            
            for (int i = 0; i < [dataArray count]; i++) {
                CourseModel *model = [[CourseModel alloc] init];
                [model setValuesForKeysWithDictionary:dataArray[i]];
                [self.dataSource addObject:model];
            }
            
            [self.tableView reloadData];
        }
            break;
            
        default:
            break;
    }
    
    
    
}

- (void)networkingHandlefail:(NetworkingHandle *)handle type:(NetWorkingType)type{
    [self hideHud];
}

- (void)networkingHandleWarning:(NetworkingHandle *)handle responseObject:(id)responseObject type:(NetWorkingType)type{
    [self hideHud];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
